package org.minuframework.file.read;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.http.HttpServletResponse;

import org.minuframework.exception.ExceptionFailed;
import org.minuframework.file.upload.AbstractFileBuilder;
import org.minuframework.file.upload.FileParamerConfigurer;
import org.minuframework.file.upload.FileTypesConfigurer;
import org.minuframework.file.upload.FileUploadType;

@Deprecated
public class DefaultFileRead extends AbstractFileBuilder implements FileReadFactory{
    
    public DefaultFileRead(FileParamerConfigurer fileParamerConfigurer, FileTypesConfigurer fileTypesConfigurer, String charset){
        super(fileParamerConfigurer, fileTypesConfigurer, charset);
    }
    
    // ------------------------------------------------------------------------
    
    /**
     * 读取图片
     * @param fileName 路径
     * @param response HttpServletResponse
     * @param type     文件类型
     * @throws ExceptionFailed
     */
    public boolean picRead(String fileName, HttpServletResponse response, FileUploadType type) throws ExceptionFailed{
        boolean flag = false;
        File file = new File(getFile(fileName, type), fileName);
        if (file.exists()) {
            picResponse(response, fileName);
            reader(response, file);
        } else {
            throw new ExceptionFailed("Cantnot find file from " + file);
        }
        return flag;
    }
    
    /**
     * 读取图片
     * @param fileName 路径
     * @param response HttpServletResponse
     * @param type     文件类型
     * @throws ExceptionFailed
     */
    public boolean picRead(String fileName, HttpServletResponse response, String type) throws ExceptionFailed{
        boolean flag = false;
        File file = new File(getFile(fileName, type), fileName);
        if (file.exists()) {
            picResponse(response, fileName);
            reader(response, file);
        } else {
            throw new ExceptionFailed("Cantnot find file from " + file);
        }
        return flag;
    }
    
    // ------------------------------------------------------------------------
    
    public void picResponse(HttpServletResponse response, String fileName){
        // 禁止图像缓存
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        // 设置response返回编码
        response.setCharacterEncoding(charset);
        // 设置输出流内容格式
        response.setHeader("Content-Type", fileTypesConfigurer.getContentType(fileName));
    }
    
    public static boolean reader(HttpServletResponse response, File file) throws ExceptionFailed{
        boolean flag = false;
        
        BufferedInputStream bis = null;
        FileInputStream fis = null;
        OutputStream os = null;
        try {
            fis = new FileInputStream(file);
            bis = new BufferedInputStream(fis);
            os = response.getOutputStream();
            
            int len = 0;
            byte[] buffer = new byte[1024 * 8];
            while ((len = bis.read(buffer)) != -1) {
                os.write(buffer, 0, len);
                os.flush();
            }
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (os != null) {
                    os.close();
                }
                if (bis != null) {
                    bis.close();
                }
                if (fis != null) {
                    fis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        
        return flag;
    }
    
}
